package edu.mobbuzz.page.menu.inbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;

import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.page.menu.InboxMenu;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.renderer.MessageRenderer;
import java.util.Vector;

public class InboxPage extends Menu{
    private Form inboxPageForm;
    private Container inboxPagePanel; 
    private Label menuTitle; 
    private int orientation;
    private List list;
    private Command backCmd;
    
    public InboxPage(){
        System.gc();
        inboxPageForm = new Form(){
            public void keyPressed(int keyCode) {
                gameActionPerformed(keyCode);
            }        
        };    
        inboxPagePanel = new Container();
        menuTitle = new Label(getMenuTitle());   
        backCmd = new Command("Back"){
            public void actionPerformed(ActionEvent evt) {
                backToInboxMenu();
            }
        };
    }
    
    public void showPage(){        
        System.gc();
        Vector contactArray = new Vector();
        contactArray.addElement(new Message("18 July 2008","Detik.Com","Penurunan Harga BBM","Pemerintah akhirnya sepakat untuk menurunkan", false));
        contactArray.addElement(new Message("satu","dua","tiga","empat", true));
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);         
        
        inboxPagePanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));        
        //inboxPagePanel.getStyle().setBgImage(Theme.getBackground());
                
        inboxPagePanel.addComponent(menuTitle);
        inboxPagePanel.addComponent(createList(contactArray, List.VERTICAL, new MessageRenderer()));
                
        inboxPageForm.setLayout(new BorderLayout());       
        inboxPageForm.addComponent(BorderLayout.CENTER, inboxPagePanel);  
        inboxPageForm.addCommand(backCmd);
        inboxPageForm.setBackCommand(backCmd);
        inboxPageForm.addCommand(new Command("Share"){
            public void actionPerformed(ActionEvent evt) {
                shareMessage();
            }
        });
        inboxPageForm.addCommand(new Command("Delete"){
            public void actionPerformed(ActionEvent evt) {
              
            }
        });
        inboxPageForm.addCommand(new Command("Open"){
            public void actionPerformed(ActionEvent evt) {
                displayMessage();
            }
        });
        inboxPageForm.setCommandListener(getActionlistener());
        inboxPageForm.setScrollable(false);
        inboxPageForm.show();
                
    }
    
    private List createList(Vector contacts, int orientation, ListCellRenderer renderer) {
        System.gc();
        list = new List(contacts);
        this.orientation = orientation;
        list.getStyle().setBgTransparency(0);
        list.getStyle().setBorder(Border.getEmpty());
        list.setListCellRenderer(renderer);
        list.setOrientation(orientation);
        
        return list;
    }
    
    public String getMenuTitle() {
        return MenuString.INBOX;
    }   
    
    public void gameActionPerformed(int keyCode){
        System.gc();
        int game = Display.getInstance().getGameAction(keyCode);
        if(game == Display.GAME_FIRE){
            displayMessage();
        }
    }
    
    public void shareMessage() {

    }

    public void displayMessage() {
         System.gc();
         Message msg = (Message)list.getSelectedItem();
         MessagePage messagePage = new MessagePage();
         messagePage.setExitCmd(getExitCmd());
         messagePage.setActionlistener(getActionlistener());
         messagePage.showPage();
         
    }
    
    public void backToInboxMenu() {
        System.gc();
        InboxMenu inboxMenu = new InboxMenu();
        inboxMenu.setExitCmd(getExitCmd());
        inboxMenu.setActionlistener(getActionlistener());
        inboxMenu.showPage();
        System.gc();
        
    }

}
 
